package com.hidt.shop.service.impl;

import com.hidt.shop.domain.AdvertisementGoods;
import com.hidt.shop.domain.Goods;
import com.hidt.shop.mapper.AdvertisementMapper;
import com.hidt.shop.mapper.SelectGoodsMapper;
import com.hidt.shop.service.AdvertisementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class AdvertisementServiceImlp   implements AdvertisementService {

    @Autowired
    private AdvertisementMapper advertisementMapper;
    @Autowired
    private SelectGoodsMapper selectGoodsMapper;
    //    增加轮播图
    public  int  addAdvertisement(Map map){
        int i = advertisementMapper.addAdvertisement(map);
        return i;
    }

    //   （伪）删除轮播图
    public  int deleteAdvertisement(Map map){
        int i = advertisementMapper.deleteAdvertisement(map);
        return i;
    }

    //      修改轮播图
    public  int updateAdvertisement(Map map){
        int i = advertisementMapper.updateAdvertisement(map);
        return i;
    }

    //    查询轮播图
    public List<AdvertisementGoods> selectAdvertisement(Map map){
        List<AdvertisementGoods> advertisementGoodsList = new ArrayList();
        //如果传过来的是有商品名，那么执行这个
        if(map.get("good_name")!=null && map.get("good_name")!=""){
//            通过名字查询商品id
            List<Goods> goodsList = selectGoodsMapper.selectGoodsByNameAndPrice(map, Integer.parseInt((String) map.get("start"))-1, 100);
//            对结果进行遍历 对商品id进行轮播图查询
            for (Goods goods : goodsList){
                map.put("goods_id",goods.getGood_id());
                List<AdvertisementGoods> advertisements = advertisementMapper.selectAdvertisement(map);
                if (advertisements.size() > 0) {
                    advertisements.get(0).setGood_name(goods.getGood_name());
                    advertisementGoodsList.add(advertisements.get(0));
                }
            }
        } else {
            advertisementGoodsList = advertisementMapper.selectAdvertisement(map);
        }
        return advertisementGoodsList;
    }
}
